package com.zhuss.order.order;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.zhuss.bean.bo.order.order.CancelOrderBo;
import com.zhuss.bean.bo.order.order.CareOrderBo;
import com.zhuss.bean.bo.order.order.CareOrderRefundBo;
import com.zhuss.bean.bo.order.order.ConfirmTakeBo;
import com.zhuss.bean.bo.order.order.CreateOrderBo;
import com.zhuss.bean.bo.order.order.CreatePayOrderRefundBo;
import com.zhuss.bean.bo.order.order.DetailOrderBo;
import com.zhuss.bean.bo.order.order.PayOrderBo;
import com.zhuss.bean.bo.order.order.app.DetailTicketBo;
import com.zhuss.bean.bo.order.order.app.ListOrderBo;
import com.zhuss.bean.bo.order.order.app.ListTicketBo;
import com.zhuss.bean.vo.order.order.CareOrderRefundVo;
import com.zhuss.bean.vo.order.order.CareOrderVo;
import com.zhuss.bean.vo.order.order.ConfirmTakeVo;
import com.zhuss.bean.vo.order.order.CreateOrderVo;
import com.zhuss.bean.vo.order.order.CreatePayOrderRefundVo;
import com.zhuss.bean.vo.order.order.DetailOrderVo;
import com.zhuss.bean.vo.order.order.PayOrderVo;
import com.zhuss.bean.vo.order.order.app.ListOrderVo;
import com.zhuss.bean.vo.order.order.app.ListTicketVo;
import com.zhuss.common.AbstractBo;
import com.zhuss.common.OrderService;
import com.zhuss.common.PageVo;
import com.zhuss.common.Processor;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.order.processor.OrderItemRead;
import com.zhuss.order.processor.OrderRead;

/**
 * @description: 订单操作的Service
 * @author: zhushanshuo
 * @create: Mon Jan 11 10:24:29 CST 2021
 **/
@Component("orderOperateService")
public class OrderOperateService implements OrderService<Object, Object> {
	
	@Autowired
	@Qualifier("orderCreate")
	private Processor<CreateOrderVo, CreateOrderBo> orderCreate;
	
	
	@Autowired
	@Qualifier("orderCancel")
	private Processor<Object, CancelOrderBo> orderCancel;
	
	@Autowired
	@Qualifier("orderPay")
	private Processor<PayOrderVo, PayOrderBo> orderPay;
	
	
	@Autowired
	@Qualifier("careOrderForUpdate")
	private Processor<CareOrderVo, CareOrderBo> careOrderForUpdate;
	
	@Autowired
	@Qualifier("careOrderRefundForUpdate")
	private Processor<CareOrderRefundVo, CareOrderRefundBo> careOrderRefundForUpdate;
	
	@Autowired
	@Qualifier("detailOrderForUpdate")
	private Processor<DetailOrderVo, DetailOrderBo> detailOrderForUpdate;
	
	@Autowired
	@Qualifier("listOrderForUpdate")
	private Processor<PageVo<ListOrderVo>, ListOrderBo> listOrderForUpdate;
	
	@Autowired
	@Qualifier("listTicketForUpdate")
	private Processor<PageVo<ListTicketVo>, ListTicketBo> listTicketForUpdate;
	
	@Autowired
	@Qualifier("takeConfirm")
	private Processor<ConfirmTakeVo, ConfirmTakeBo> takeConfirm;
	
	@Autowired
	@Qualifier("orderRefundCreate")
	private Processor<CreatePayOrderRefundVo, CreatePayOrderRefundBo> orderRefundCreate;
	
	
	@Autowired
	private OrderRead orderRead;
	
	@Autowired
	private OrderItemRead orderItemRead;
	
	
	/**
	 * 创建
	 */
	@Override
    @Transactional(rollbackFor = Exception.class)
	public CreateOrderVo create(Object param) throws DzcxException {
		
		CreateOrderBo bo = (CreateOrderBo) param;
		
		CreateOrderVo result = orderCreate.process(bo);
		
		return result;
	}
	
	/**
	 * 支付
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public PayOrderVo pay(Object param) throws DzcxException {
		
		PayOrderBo bo = (PayOrderBo) param;
		
		PayOrderVo result = orderPay.process(bo);
		
		return result;
	}
	
	/**
	 * 取消
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Void cancel(Object param) throws DzcxException {
		
		CancelOrderBo bo = (CancelOrderBo) param;
		
		orderCancel.process(bo);
		
		return null;
	}
	
	/**
	 * 查询刷新
	 * @param param
	 * @return
	 * @throws DzcxException
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public CareOrderVo careOrderForUpdate(Object param) throws DzcxException {
		
		CareOrderBo bo = (CareOrderBo) param;
		
		CareOrderVo result = careOrderForUpdate.process(bo);
		
		return result;
	}
	
	/**
	 * 确认上车
	 * @param param
	 * @return
	 * @throws DzcxException
	 */
	@Override
	public Object confirmTake(Object param) throws DzcxException {
		
		ConfirmTakeBo bo = (ConfirmTakeBo) param;
		
		ConfirmTakeVo result = takeConfirm.process(bo);
		
		return result;
	}
	
	@Override
	public Object detailForUpdate(Object param) throws DzcxException {
		DetailOrderBo bo = (DetailOrderBo) param;
		DetailOrderVo result = detailOrderForUpdate.process(bo);
		return result;
	}
	
	@Override
	public Object listOrderForUpdate(Object param) throws DzcxException {
		ListOrderBo bo = (ListOrderBo) param;
		Object result = listOrderForUpdate.process(bo);
		return result;
	}
	
	@Override
	public Object listTicketForUpdate(Object param) throws DzcxException {
		ListTicketBo bo = (ListTicketBo) param;
		Object result = listTicketForUpdate.process(bo);
		return result;
	}
	
	@Override
	public Object listTicket(Object param) throws DzcxException {
		ListTicketBo bo = (ListTicketBo) param;
		PageVo<ListTicketVo> result = (PageVo<ListTicketVo>) orderItemRead.listTicket(bo);
		return result;
	}
	
	@Override
	public Object detailTicket(Object param) throws DzcxException {
		DetailTicketBo bo = (DetailTicketBo) param;
		Object result = orderRead.detailTicket(bo);
		return result;
	}
	
	
	
	/**
	 * 退款
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public CreatePayOrderRefundVo refund(Object param) throws DzcxException {
		
		CreatePayOrderRefundBo bo = (CreatePayOrderRefundBo) param;
		
		CreatePayOrderRefundVo result = orderRefundCreate.process(bo);
		
		return result;
	}
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public CareOrderRefundVo careOrderRefundForUpdate(Object param) throws DzcxException {
		
		CareOrderRefundBo bo = (CareOrderRefundBo) param;
		
		CareOrderRefundVo result = careOrderRefundForUpdate.process(bo);
		
		return result;
	}
	
	@Override
	public Object latestTicket(Object param) throws DzcxException {
		AbstractBo bo = (AbstractBo) param;
		ListTicketVo result = orderItemRead.latestTicket(bo.getConcurrentUserId());
		if (result == null) {
			throw new DzcxException(ExceptionCode.暂无车票);
		}
		return result;
	}

	
}

